<template>
  <div>
    <el-dialog title="编辑课程体系" :visible="true" width="80%" @close="handleClose">
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="name" label="名称" width="180">
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="180">
        </el-table-column>
        <el-table-column prop="updated_at" label="更新时间" width="180">
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <span class="toolbar-btn" @click="handleClickEdit(scope.row)">编辑</span>
            <span class="toolbar-btn" @click="handleClickDelete(scope.row)">删除</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <first-level-edit v-if="showFirstEdit" :id="id" @close="handleCloseEdit" @success="handleSuccess"></first-level-edit>
  </div>
</template>

<script>
import { getCourseLevel, deleteCourseLevel } from "@/api/course";
import FirstLevelEdit from './FirstLevelEdit.vue'
export default {
   components:{
    FirstLevelEdit
  },
  data() {
    return {
      showFirstEdit:false,
      id:'',
      list: [],
      form: {
        name: ''
      }

    }
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      let res = await getCourseLevel();
      this.list = res.data;
    },
    async deleteCourseLevel(id) {
      let res = await deleteCourseLevel({ id });
      this.getList();
      this.$message({
        type: 'success',
        message: '删除成功!'
      });
    },
    handleClickEdit(item){
      this.id = item.id;
      this.showFirstEdit = true;
    },
    handleClickDelete(item) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteCourseLevel(item.id)
      })
    },
    handleClose() {
      this.$emit('close')
    },
    handleSuccess(){
      this.showFirstEdit = false;
      this.getList();
    },
    handleAdd() {
      if (this.form.name === '') {
        this.$message({
          message: '请输入课程体系名称！',
          type: 'warning'
        });
      } else {
        this.addCourseLevel();
      }
    },
    handleCloseEdit(){
      this.showFirstEdit = false
    }
  },
}
</script>
<style lang="scss" scoped>
</style>
